/************************************************************************/
/* Name : Kiwon Yun
/* Partner's name : Eric Rooney
/* File Name : singleton.h
/* The name of assignment : Iteration 2
/************************************************************************/

#ifndef SINGLETON_H_
#define SINGLETON_H_

/************************************************************************/
/* This template class is for singleton objects.
/* The Singleton object can be used when just one object have to be exist in a program.
/* Usage : class CLASS_NAME : public Singleton<CLASS_NAME>
/************************************************************************/

#pragma warning (disable:4311)
#pragma warning (disable:4312)

template<class T>
class Singleton
{
public:
	static T& GetObj()
	{
		if( !m_pObject )
			CreateSingleton();

		return (*m_pObject);
	}

	static T* GetObjPtr()
	{
		if( !m_pObject )
			CreateSingleton();

		return m_pObject;
	}

	static void DestroySingleton()
	{
		delete m_pObject;
		m_pObject = NULL;
	}
protected:
	Singleton()
	{
		int offset = (int)(T*)1-(int)(Singleton<T>*)(T*)1; // for multiple inheritances
		m_pObject = (T*)((int)this+offset);
	}

	virtual ~Singleton()
	{
		m_pObject = NULL;
	}

private:
	static bool CreateSingleton()
	{
		T* pTemp = new T;
		return true;
	}

	static T* m_pObject;
};

template <class T> T* Singleton<T>::m_pObject = 0;

#endif // _SINGLETON_H_




